********************************************************************************
* "Political Accountability and Democratic Institutions: An Experimental Assessment"
* By Jose Gabriel Castillo and John Hamman
* Journal of Experimental Political Science
* 11/12/2019
* Final program single choice
********************************************************************************

use data1.dta, clear //data set is create in stata 13 version

********************************************************************************
* Generate dummies for DD 
********************************************************************************
gen puntreat=0				// punishment dummy: second phase
replace puntreat=1 if phase == 2
label var puntreat "punishment (P)"

gen demtreat=0				// Democracy treatment 
replace demtreat=1 if treatment == 2
label var demtreat "Democracy (D)"

gen dddem=puntreat*demtreat //D-i-D: Pun*Dem
label var dddem "Democracy vs. Leviathan (P*D)"

********************************************************************************
* New variables for figures
********************************************************************************
xtset subject period

by group period, sort : egen float ave_groupcontribution = mean(contribution)
label var ave_groupcontribution "Average group contribution"

gen ave_Othercontribution = (5*ave_groupcontribution - contribution)/4
label var ave_Othercontribution "Average Fellow contribution"

by institution period, sort : egen float ave_institutioncontribution = mean(contribution)
label var ave_institutioncontribution "Average contribution in Each institution"

by institution4 period, sort : egen float ave_institution4contribution = mean(contribution)
label var ave_institution4contribution "Average contribution in each sub-Treatment"

by institution4 period, sort : egen float ave_institution4contributionMan = mean(contribution) if manager2==1
label var ave_institution4contributionMan "Average Manager contribution in each sub-Treatment"

by institution4 period, sort : egen float ave_institution4contributionFel = mean(contribution) if manager2==0
label var ave_institution4contributionFel "Average non-Managers contribution in each sub-Treatment"

by treatment period, sort : egen float ave_treatmentcontribution = mean(contribution)
label var ave_treatmentcontribution "Average contribution in each Treatment"

by institution period, sort : egen float ave_institutionpunishment = mean(punishment)
label var ave_institutionpunishment "Average punishment in each institution"

by group period, sort : egen float ave_groupprofit = mean(profit)
label var ave_groupprofit "Average group profit"

by institution period, sort : egen float ave_institutionprofit = mean(profit)
label var ave_institutionprofit "Average profit in each institution"

by institution4 period, sort : egen float ave_institution4profit = mean(profit)
label var ave_institution4profit "Average profit in each sub-Treatment"

by treatment period, sort : egen float ave_Treatmentprofit = mean(profit)
label var ave_Treatmentprofit "Average profit in each Treatment"

by group period, sort : egen float ave_groupprofit_nopunished = mean(profit) if punished!=1
label var ave_groupprofit_nopunished "Average group profit excluding punished subjects"

by institution period, sort : egen float ave_institutionprofit_nopunished = mean(profit) if punished!=1
label var ave_institutionprofit_nopunished "Average profit in each institution excluding punished subjects"

by institution4 period, sort : egen float ave_institution4profit_nopunish = mean(profit) if punished!=1
label var ave_institution4profit_nopunish "Average profit in each sub-Treatment excluding punished subjects"

by treatment period, sort : egen float ave_treatmentprofit_nopunished = mean(profit) if punished!=1
label var ave_treatmentprofit_nopunished "Average profit in each Treatment excluding punished subjects"

********************************************************************************
* Complementary programs
********************************************************************************

********************************************************************************
* Figure 1: Contribution Trend *
********************************************************************************
xtline ave_institution4contribution, overlay ///
plot1opts(recast(connected) msymbol(o) lpattern(shortdash) lc(gs8) mc(gs8))  ///
plot2opts(recast(connected) msymbol(o) lpattern(solid) lc(black) mc(black))  ///
plot3opts(recast(connected) msymbol(s) lpattern(shortdash) lc(gs8) mc(gs8))  ///
plot4opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black))  /// 
ytitle(Average Contribution (Points)) ttitle(period) legend(on) i(institution4) t(period) ///
ylabel(0(2)20, nogrid) xlabel(0(1)20)  scheme(s1mono) xline(11, lpattern(dash)) ///
legend(label(1 "Leviathan (VCM)") label(2 "Democracy (VCM)") label(3 "Leviathan (Punish.)") ///
label(4 "Democracy (Punish.)"))

graph save Graph fig1s, replace
graph export fig1s.eps, replace

********************************************************************************
* Table 3: Average Performance Comparison
* Note: The differences that stata windows results shows are VCM - Punishment. 
*       In table 3 we report Punishment - VCM results. 
********************************************************************************
*Panel A:
preserve
collapse(mean) treatment contribution, by(group phase)
xtset group phase
* contribution pre vs. post in Leviathan
ttest contribution if treatment == 1, by (phase)
ranksumex contribution if treatment == 1, by (phase)
* contribution pre vs. post in Democracy
ttest contribution if treatment == 2, by (phase)
ranksumex contribution if treatment == 2, by (phase)
* Diff-in-Diff in contribution
gen dcont=contribution-L.contribution
ttest dcont, by(treatment)
ranksumex dcont, by (treatment)
restore

** Panel B:
* Punishment Leviathan vs. Democracy
preserve
collapse(mean) treatment punishment manager2, by(group phase)
ttest punishment if phase == 2, by(treatment) 
ranksumex punishment if phase == 2, by(treatment) 
restore

** Panel C
preserve
collapse(mean) treatment profit, by(group phase)
* Profit pre vs. post in Leviathan
ttest profit if treatment == 1, by (phase)
ranksumex profit if treatment == 1, by (phase)
* Profit pre vs. post in Democracy
ttest profit if treatment == 2, by (phase)
ranksumex profit if treatment == 2, by (phase)
* Diff-in-Diff in Profit
sort group
xtset group phase
gen dprof=profit-L.profit
ttest dprof, by(treatment)
ranksumex dprof, by (treatment)
restore

********************************************************************************
* Figure 2: Punishment Behavior
********************************************************************************

* Figure 2.1: Punishment Assigned in single choice *
xtline ave_institutionpunishment if period >10, ///
overlay plot1opts(recast(connected) msymbol(o) lpattern(dash) lc(gs8) mc(gs8)) ///
plot2opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black)) ///
ytitle(Average Punishment (Points)) ttitle(period) legend(on) i(institution) t(period) ///
ylabel(0(1)3, nogrid)xlabel(10(1)20, nogrid) scheme(s1mono) ///
legend(label(1 "Leviathan") label(2 "Democracy")) 

graph save Graph fig2.1s, replace
graph export fig21s.eps, replace

by treatment period group, sort: egen float punish_fr = mean(punished)
by treatment period group, sort: gen float manager_pun = (punish_fr>0 & punish_fr!=.)
by treatment period, sort: egen float manager_pun_fr = mean(manager_pun)
gen manager_pun_freq=100*manager_pun_fr

* Figure 2.2: Manager's use of punishment in single choice*
xtline manager_pun_freq if period >10, overlay plot1opts(recast(connected) ///
msymbol(o) lpattern(dash) lc(gs8) mc(gs8)) ///
plot2opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black)) ///
ytitle(% of managers that use punishment) ttitle(period) legend(on) i(institution) ///
t(period) ylabel (0(20)100, nogrid) xlabel (10(1)20, nogrid) scheme(s1mono) ///
legend(label(1 "Leviathan") label(2 "Democracy")) 

graph save Graph fig2.2s, replace
graph export fig22s.eps, replace

* Figure 2.3: Punished subjects frecuency by group
by treatment period group, sort: egen float subject_punished = sum(punished)
by treatment period, sort: egen float subject_punfr = mean(subject_punished)
xtline subject_punfr if period >10, overlay plot1opts(recast(connected) ///
msymbol(o) lpattern(dash) lc(gs8) mc(gs8)) ///
plot2opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black)) ///
ytitle("Average Number of Punished Subjects by Group") ttitle(period) legend(on) i(institution) ///
t(period) ylabel (0(0.5)3, nogrid) xlabel (10(1)22, nogrid) scheme(s1mono) ///
legend(label(1 "Leviathan") label(2 "Democracy")) 

graph save Graph fig2.3, replace
graph export fig23s.eps, replace


********************************************************************************
* Table 4 Determinants of contributions(SE clustered by group) *****************
********************************************************************************

* Preparation for regression analysis
* Demographic controls
global control1 woman economist height age // no missing
global control2  income bret public_school insurance other_city risk_aversion ///
				 // some missing

* lag *
bys subject (period): gen contribution_l = contribution[_n - 1]
label var contribution_l "contribution (t-1)"

bys subject (period): gen ave_Othercontribution_l = ave_Othercontribution[_n - 1]
label var ave_Othercontribution_l "Other members' Av.contribution(t-1)"

bys subject (period): gen punishment_l = punishment[_n - 1] // points assigned to administrator
replace punishment_l=0 if missing(punishment_l)
label var punishment_l "Punishment in the group (t-1)"

bys subject (period): gen punished_l = punished[_n - 1] // receibes punishment
replace punished_l=0 if missing(punished_l)
label var punished_l "Punishment Received (t-1)"

qui tab subject, gen(IDcode)
local i=1
while (`i'<=200){
qui gen IDperiod`i' = IDcode`i'*period // 
label variable IDperiod`i' "subject-Specific Time Trend for IDcode`i'"
local i = `i' + 1
}
qui tab group, gen(groupcode)
local i=1
while (`i'<=40){
qui gen groupperiod`i' = groupcode`i'*period // periodinphase
label variable groupperiod`i' "group-Specific Time Trend for groupcode`i'"
local i = `i' + 1
}
********************************************************************************
*periodinphase
********************************************************************************
gen period1=period if phase==1
replace period1=0 if missing(period1)
gen period2=period-10 if phase==2
replace period2=0 if missing(period2)
gen periodinphase=period1+period2
label var periodinphase "Period within phase"

xtset subject period

* Regresions					
eststo FEgt: xi: reg contribution dddem puntreat demtreat  ///
					i.subject groupcode* periodinphase, ///
					vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"
					
eststo FEgtc: xi: reg contribution dddem puntreat demtreat  ///
					i.subject groupcode* ///
					ave_Othercontribution_l punishment_l punished_l, ///
					vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"
					
esttab FEgt FEgtc using tab4s.tex, replace /// Table size adjusted manually with \adjustbox
				title("Determinants of contributions: regression results" \label{tab4}) ///
				label mti b(4) se(4) star(* 0.10 ** 0.05 *** 0.01) ///
				obslast keep(dddem puntreat demtreat ave_Othercontribution_l punishment_l punished_l) ///
				scalars("cont Other Controls" "ife Individual FE" "gfe group FE" "trend Trend within phase" "r2 R-squared" "N Observations") ///
				nonotes addnotes("Notes: Dependent variable=contributions (ECU)." "Other controls include: gender, economics's student, parent's education and labor status, and income related information," "member of student union, whether the student is a single child, has a scholarship, has a job, ethnicity, partymember and student leader." "Standard errors clustered at group level in parenthesis." "\hspace{.2cm} \sym{*} Significant at the  10 percent level." "\hspace{.05cm} \sym{**} Significant at the \hspace{.05cm} 5 percent level." "\sym{***} Significant at the \hspace{.05cm} 1 percent level.")

est clear

********************************************************************************
*Table 5: Punishing decisions 
********************************************************************************
gen devothers = contribution - ave_Othercontribution
label var devothers "OMC deviation"
gen absdev = abs(devothers)
gen posdev = devothers if devothers>0
replace posdev = 0 if posdev ==.
label var posdev "OMC positive deviation"
gen negdev = abs(devothers) if devothers<0
replace negdev = 0 if negdev ==.
label var negdev "OMC negative deviation"

xtset subject period
				
eststo re2: xi: qui xtreg punishment demtreat negdev posdev if punished, ///
				re vce(cluster group)
eststo punishb: qui xtprobit punished demtreat negdev posdev if puntreat, pa
				margins, dydx(*)

//Values in table from marginal effects, manually imputed
esttab punishb re2 using tab5s.tex, replace /// Table size adjusted manually with \adjustbox
				title("Punishment decisions" \label{tab6}) ///
				stats(r2_o N, fmt(%9.3f %9.0g) labels("R-squared (overall)" Observations)) ///
				mtitles("Punished=1" "Punished=1" "Punishment Pts." "Punishment Pts.") ///
				label mti b(4) se(4) star(* 0.10 ** 0.05 *** 0.01) ///
				keep(demtreat negdev posdev) ///
				scalars("cont Other Controls" "r2 R-squared" "N Observations") ///
				nonotes addnotes("Notes: Coefficients for first 2 models represent the average marginal effects of the probability of being" "punished, for a Panel Data Probit model that captures the withing individual correlation." "Standard errors clustered at group level in parenthesis for models 3 and 4." "\hspace{.2cm} \sym{*} Significant at the  10 percent level." "\hspace{.05cm} \sym{**} Significant at the \hspace{.05cm} 5 percent level." "\sym{***} Significant at the \hspace{.05cm} 1 percent level.")
est clear

********************************************************************************
* Table 6: Leadership contribution analysis
******************************************************************************** 
preserve
xtset subject period
gen d10c = contribution - l10.contribution // c(t)-c(t-10)

* Punishment - VCM, overall
ttest d10c, by (manager2)
ranksumex d10c, by (manager2)
* Punishment - VCM, Leviathan
ttest d10c if institution4 == 3, by (manager2)
ranksumex d10c if institution4 == 3, by (manager2)
* Punishment - VCM, Democracy
ttest d10c if institution4 == 4, by (manager2)
ranksumex d10c if institution4 == 4, by (manager2)

********************************************************************************
* Figure 3: Contribution's dynamics by roles
********************************************************************************

gen mean_managercont=.
forvalues i=1/20{
qui mean contribution if treatment==1 & manager2==1 & period==`i' 
matrix c=e(b)
replace mean_managercont=c[1,1] in `i'
}
gen mean_nonmanagercont=.
forvalues i=1/20 {
qui mean contribution if treatment==1 & manager2==0 & period==`i'
matrix c=e(b)
replace mean_nonmanagercont=c[1,1] in `i'
}

gen mean_managercontd=.
forvalues i=1/20{
qui mean contribution if treatment==2 & manager2==1 & period==`i' 
matrix c=e(b)
replace mean_managercontd=c[1,1] in `i'
}
gen mean_nonmanagercontd=.
forvalues i=1/20 {
qui mean contribution if treatment==2 & manager2==0 & period==`i'
matrix c=e(b)
replace mean_nonmanagercontd=c[1,1] in `i'
}

gen per=_n
twoway (connected mean_managercont per if per<=10, msymbol(O) lpattern(solid) lc(ltblue) mc(ltblue)) ///
(connected mean_managercont per if per>10 & per<=20, msymbol(Oh) lpattern(solid) lc(ltblue) mc(ltblue)) ///
(connected mean_nonmanagercont per if per<=10, msymbol(O) lpattern(shortdash) lc(gs8) mc(gs8)) ///
(connected mean_nonmanagercont per if per>10 & per<=20, msymbol(Oh) lpattern(shortdash) lc(gs8) mc(gs8)) ///
(connected mean_managercontd per if per<=10, msymbol(T) lpattern(solid) lc(blue) mc(blue)) ///
(connected mean_managercontd per if per>10 & per<=20, msymbol(Th) lpattern(solid) lc(blue) mc(blue)) ///
(connected mean_nonmanagercontd per if per<=10, msymbol(T) lpattern(shortdash) lc(black) mc(black)) ///
(connected mean_nonmanagercontd per if per>10 & per<=20, msymbol(Th) lpattern(shortdash) lc(black) mc(black)), ///
ytitle("Average Contribution (Points)") xtitle("period") legend(label(1 "Manager's-Leviathan (VCM)") label(2 "Manager's-Leviathan (Punish.)") ///
label(3 "Non-manager's-Leviathan (VCM)") label(4 "Non-manager's-Leviathan (Punish.)") label(5 "Manager's-Democracy (VCM)") label(6 "Manager's-Democracy (Punish.)") ///
label(7 "Non-manager's-Democracy (VCM)") label(8 "Non-manager's-Democracy (Punish.)") size(small) symxsize(8) keygap(1)) scheme(s1mono) ///
xlabel(0(1)20) ylabel(0(2)20, nogrid) xline(11, lpattern(dash))

graph save Graph fig3s, replace
graph export fig3s.eps, replace

********************************************************************************
* Figure 4: Manager�s contribution rank within group
******************************************************************************** 

*** Rank cumulative contribution in VCM phase within group
bys subject phase: egen Cumulativecontribution = sum(contribution) if period<=10

bys group period: egen rank_contribution = rank(Cumulativecontribution) if period<=10, field

gen manrank = rank_contribution if manager2 == 1 & period == 10
label var manrank "Rank of managers' cumulative contributions in VCM"
histogram manrank, scheme(s1mono) discrete width(1) frequency addlabel ylabel(0(10)30, nolabels) ymtick(, nolabels) ///
xlabel(0(1)5, nolabels) by(, legend(off)) by(treatment, style(combine))
gr_edit plotregion1.graph1.xaxis1.title.draw_view.setstyle, style(no)
gr_edit plotregion1.graph2.xaxis1.title.draw_view.setstyle, style(no)
gr_edit note.draw_view.setstyle, style(no)
gr_edit plotregion1.graph1.subtitle.text = {}
gr_edit plotregion1.graph1.subtitle.text.Arrpush Leviathan
gr_edit plotregion1.graph2.subtitle.text = {}
gr_edit plotregion1.graph2.subtitle.text.Arrpush Democracy

graph save Graph fig4s, replace
graph export fig4s.eps, replace

********************************************************************************
* Additional tables and figures
********************************************************************************

* Onlinesupmaterial

********************************************************************************
* Figure 1: Profit�s dynamics
********************************************************************************

* Figure 1.1: Average Profit (Points) 
xtline ave_institution4profit, overlay ///
plot1opts(recast(connected) msymbol(o) lpattern(shortdash) lc(gs8) mc(gs8)) ///
plot2opts(recast(connected) msymbol(o) lpattern(solid) lc(black) mc(black)) ///
plot3opts(recast(connected) msymbol(s) lpattern(shortdash) lc(gs8) mc(gs8)) ///
plot4opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black)) ///
ytitle(Average Profit (Points)) ttitle(period) legend(on) i(institution4) t(period) ylabel(24(2)30, nogrid) ///
xlabel(0(1)20)  scheme(s1mono) ///
legend(label(1 "Leviathan (VCM)") label(2 "Democracy (VCM)") label(3 "Leviathan (Punish.)") ///
label(4 "Democracy (Punish.)"))

graph save Graph addfig1s, replace
graph export addfig11s.eps, replace

* Figure 1.2: Average Profit (Points)-Unpunished subjects
xtline ave_institution4profit_nopunish, overlay ///
plot1opts(recast(connected) msymbol(o) lpattern(shortdash) lc(gs8) mc(gs8)) ///
plot2opts(recast(connected) msymbol(o) lpattern(solid) lc(black) mc(black)) ///
plot3opts(recast(connected) msymbol(s) lpattern(shortdash) lc(gs8) mc(gs8)) ///
plot4opts(recast(connected) msymbol(s) lpattern(solid) lc(black) mc(black)) ///
ytitle(Average Profit (Points)-Unpunished subjects) ttitle(period) legend(on) i(institution4) t(period) ylabel(24(2)30, nogrid) ///
xlabel(0(1)20)  scheme(s1mono) ///
legend(label(1 "Leviathan (VCM)") label(2 "Democracy (VCM)") label(3 "Leviathan (Punish.)") label(4 "Democracy (Punish.)"))

graph save Graph adfig1.2s, replace
graph export addfig12s.eps, replace

********************************************************************************
* Table 1: Profits, regression results
********************************************************************************
xtset subject period
					
eststo FEgt: xi: reg profit dddem puntreat demtreat  ///
					i.subject groupcode* periodinphase, ///
					vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"
					
eststo FEgtc: xi: reg profit dddem puntreat demtreat  ///
					i.subject groupcode* periodinphase ///
					ave_Othercontribution_l punished_l punishment_l, ///
					vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"
					
esttab FEgt FEgtc using addtab1s.tex, replace /// Table size adjusted manually with \adjustbox
					title("Profits: regression results" \label{tab7}) ///
					label mti b(4) se(4) star(* 0.10 ** 0.05 *** 0.01) ///
					obslast keep(dddem puntreat demtreat ave_Othercontribution_l punished_l punishment_l) ///
					scalars("cont Other Controls" "ife Individual FE" "gfe group FE" "trend Trend within phase" "r2 R-squared" "N Observations") ///
					nonotes addnotes("Notes: Dependent variable=Profits(ECU)." "Other controls include: gender, economics's student, parent's education and labor status, and income related information," "member of student union, whether the student is a single child, has a scholarship, has a job, ethnicity, partymember and student leader." "Standard errors clustered at group level in parenthesis." "\hspace{.2cm} \sym{*} Significant at the  10 percent level." "\hspace{.05cm} \sym{**} Significant at the \hspace{.05cm} 5 percent level." "\sym{***} Significant at the \hspace{.05cm} 1 percent level.")

est clear

********************************************************************************
* Table 2: Determinants of contributions conditional on manager�s contribution rank
******************************************************************************** 

*** Rank cumulative contribution in VCM phase within group

bys group: egen temp = sum(manrank)
*replace temp = 1 if temp == 0
replace manrank = temp // manrank = 1,2,3,4,5
drop temp

tab manrank, gen(manrankcode)
gen DD1 = dddem*manrankcode1
label var DD1 "Democracy vs. Leviathan (MR=1)"
gen DD2 = dddem*manrankcode2
label var DD2 "Democracy vs. Leviathan (MR=2)"
gen DD3 = dddem*manrankcode3
label var DD3 "Democracy vs. Leviathan (MR=3)"
gen DD4 = dddem*manrankcode4
label var DD4 "Democracy vs. Leviathan (MR=4)"
gen DD5 = dddem*manrankcode5
label var DD5 "Democracy vs. Leviathan (MR=5)"

xtset subject period

eststo FEgt: xi: reg contribution DD* puntreat demtreat i.subject groupcode* periodinphase, vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"

eststo FEgtc: xi: reg contribution DD* puntreat demtreat i.subject groupcode* periodinphase ///
					ave_Othercontribution_l punished_l punishment_l, ///
					vce(cluster group)
					estadd local cont "No"
					estadd local ife "Yes"
					estadd local gfe "Yes"
					estadd local trend "Yes"

esttab FEgt FEgtc using addtab2s.tex, replace /// Table size adjusted manually with \adjustbox
					title("Determinants of contributions conditional on manager's contribution Rank" \label{tab5}) ///
					label mti b(4) se(4) star(* 0.10 ** 0.05 *** 0.01) ///
					obslast keep(DD* puntreat demtreat ave_Othercontribution_l punished_l punishment_l) ///
					scalars("cont Other Controls" "ife Individual FE" "gfe group FE" "trend Trend within phase" "r2 R-squared" "N Observations") ///
					nonotes addnotes("Notes: Dependent variable=contributions(ECU). MR: Manager's rank of cumulative contribution in the first phase" "Other controls include: gender, economics's student, parent's education and labor status, and income related information," "member of student union, whether the student is a single child, has a scholarship, has a job, ethnicity, partymember and student leader." "Standard errors clustered at group level in parenthesis." "\hspace{.2cm} \sym{*} Significant at the  10 percent level." "\hspace{.05cm} \sym{**} Significant at the \hspace{.05cm} 5 percent level." "\sym{***} Significant at the \hspace{.05cm} 1 percent level.")

est clear


